import json

from django.shortcuts import render
from django.http.response import HttpResponse
from achievement.models import Achievement
from student.models import Student
from course.models import Course
from django.core import serializers


# Create your views here.
class Score:
    def __init__(self, student_id, student_name, course_id, course_name, score):
        self.student_id = student_id
        self.student_name = student_name
        self.course_id = course_id
        self.course_name = course_name
        self.score = score

    def __str__(self):
        return str({
            "studentid": self.student_id,
            "studentname": self.student_name,
            "courseid": self.course_id,
            "coursename": self.course_name,
            "score": self.score
        })


def getGrade(request):
    grades = []
    data = json.loads(request.body)
    if not data.get("id"):
        return HttpResponse("请输入学号")
    try:
        a = Achievement.objects.filter(student_number=data.get("id"))
        for i in a:
            g = str(Score(i.student_number.student_number, i.student_number.student_name, i.course_number.course_number,
                          i.course_number.course_name, i.course_grade))
            grades.append(g)
        return HttpResponse(json.dumps(grades))
    except BaseException as e:
        print(e)
        return HttpResponse("查询错误")


def getGrades(request):
    grades = []
    try:
        all_grades = Achievement.objects.all()
        for i in all_grades:
            # student_name = Student.objects.filter(student_number=i.student_number)[0].student_name
            # course_name = Course.objects.filter(course_number=i.course_number)[0].course_name
            g = str(Score(i.student_number.student_number, i.student_number.student_name, i.course_number.course_number,
                          i.course_number.course_name, i.course_grade))
            grades.append(g)
        return HttpResponse(json.dumps(grades))
    except BaseException as e:
        print(e)
        return HttpResponse("查询失败")


def addGrade(request):
    if request.method != "POST":
        return
    data = json.loads(request.body)
    try:
        student_number = data.get("studentid")
        course_number = data.get("courseid")
        query = Achievement.objects.filter(student_number=student_number, course_number=course_number)
        if len(query) > 0:
            return HttpResponse("该成绩已存在")
        student = Student.objects.filter(student_number=student_number)[0]
        course = Course.objects.filter(course_number=course_number)[0]
        a = Achievement.objects.create(student_number=student,
                                       course_number=course,
                                       course_grade=data.get("score"))
        a.save()
        return HttpResponse("添加成功")
    except BaseException as e:
        print(e)
        return HttpResponse("添加失败")


def deleteGrade(request):
    data = json.loads(request.body)
    try:
        student_number = data.get("studentid")
        course_number = data.get("courseid")
        query = Achievement.objects.filter(student_number=student_number, course_number=course_number)
        if len(query) < 0:
            return HttpResponse("该成绩不存在")
        query.delete()
        return HttpResponse('删除成功')
    except:
        return HttpResponse('删除失败')


def changeGrade(request):
    data = json.loads(request.body)
    try:
        student_number = data.get("studentid")
        course_number = data.get("courseid")
        query = Achievement.objects.filter(student_number=student_number, course_number=course_number)
        if len(query) < 1:
            return HttpResponse('该成绩不存在')
        query.update(
            course_grade=data.get("score")
        )
        return HttpResponse("修改成功")
    except BaseException as e:
        print(e)
        return HttpResponse("修改失败")
